<template>
	<view>
		<cu-custom bgColor="bg-blue" :isBack="true"><block slot="content">地址管理</block></cu-custom>
		<view class="padding">
			<view class="cu-form-group margin-top">
				<view class="title">姓名</view>
				<input placeholder="请输入姓名" name="input" v-model="name"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">电话</view>
				<input placeholder="请输入电话号码" name="input" v-model="phone"></input>
			</view>
			<view class="cu-form-group align-start">
				<view class="title">详细地址</view>
				<textarea placeholder="请输入详细地址" v-model="detail"></textarea>
			</view>
			<view class="cu-form-group">
				<view class="title">是否默认</view>
				<switch @change="changeDefault" :class="selected==1?'checked':''" :checked="selected==1?true:false"></switch>
			</view>
			<view style="width: 100%;height: 40upx;"></view>
			<view class="flex flex-direction">
				<button class="cu-btn bg-green lg text-bold text-l"  style="line-height: 2.8em;width: 50%;margin-left: 25%;" @click="saveAddress()">保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selected: 0,
				name:'',
				phone:'',
				detail:'',
				id:0,
			};
		},
		onLoad(options) {
			let _this = this;
			if(options.id != null){
				_this.id = options.id;
				uni.request({
					url: _this.$globals.apibase+'/api/Luckorder/addressDetail',
					data: {
						token: uni.getStorageSync('token'),
						id:_this.id,
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if(res.data.code == '401'){
							uni.setStorageSync('token', '');
							_this.$uniApi.trylogin();
						}
						if(res.data.code == '1'){
							_this.name = res.data.data.desc.name;
							_this.phone = res.data.data.desc.phone;
							_this.detail = res.data.data.desc.detail;
							_this.selected = res.data.data.desc.selected;
						}
					}
				});
			}
		},
		onShow() {
		},
		methods: {
			changeDefault(e) {
				if(e.detail.value == true){
					this.selected = 1;
				}
				if(e.detail.value == false){
					this.selected = 0;
				}
			},
			saveAddress(){
				if (this.phone && /^1[3456789]\d{9}$/.test(this.phone)) {
				}else {
					uni.showToast({
						icon: 'none',
						title: '请先输入正确的手机号！'
					});
					return;
				}
				if(this.name == ''){
					uni.showToast({
						icon: 'none',
						title: '请输入姓名!'
					});
					return;
				}
				if(this.detail == ''){
					uni.showToast({
						icon: 'none',
						title: '请输入详细地址!'
					});
					return;
				}
				let _this = this;
				uni.request({
					url: _this.$globals.apibase+'/api/Luckorder/operaAddress',
					data: {
						token: uni.getStorageSync('token'),
						name:_this.name,
						phone:_this.phone,
						detail:_this.detail,
						selected:_this.selected,
						id:_this.id,
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if(res.data.code == '401'){
							uni.setStorageSync('token', '');
							_this.$uniApi.trylogin();
						}
						if(res.data.code == '1'){
							uni.navigateBack();
						}
						
					}
				});
			},
		}
	}
</script>

<style>
	.fl{
		float: left;
	}
	.fr{
		float: right;
	}
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
